package com.atguigu.gmallpublisher.controller;

import com.atguigu.gmallpublisher.service.SugarService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/*
把具体方法的网页访问地址给sugar，sugar访问该地址，会调用springboot的该方法，然后查询clickhouse的数据，
查询结果是以json字符串包装返回的，sugar会根据这个json字符串转化为可视化图形

*/
//controller调用service,service调用mapper数据层
@RestController//相当于在所有方法上加了@ResponseBody:可以不返回页面
//@Controller
@RequestMapping("/sugar")
public class SugarController {

    @Autowired//会找接口的实现类
    private SugarService sugarService;//这里没有警告,因为这个接口有实现类

/*
{
  "status": 0,
  "msg": "",
  "data": 1201079.8080433102
}
我们要将查询的结果给data,然后将整个json返回
 */
    @RequestMapping("/gmv")//http://localhost:8070/sugar/gmv?date=20220223
    public String getGmv(@RequestParam(value = "date",defaultValue = "0") int date){
        //我们后面要上sugar页面能自动显示当天的查询结果,而不是我们手动的每天改成当天日期
        if (date==0){//如果用户date不赋值,就给默认值0,如果等于0,我们这边就给date赋值为今天日期
            date=getToday();
        }

        //获取查询的结果
        BigDecimal gmv = sugarService.getGmv(date);
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": " +gmv+
                "}";

    }
@RequestMapping("/gmvByTm")
    public String getGmvByTm(@RequestParam(value = "date",defaultValue = "0") int date,
                             @RequestParam(value = "limit",defaultValue = "5") int limit){
        if (date==0){
            date=getToday();
        }

        //查询数据
        Map gmvByTm = sugarService.getGmvByTm(date, limit);

        Set tmNames = gmvByTm.keySet();
        Collection tmGmvs = gmvByTm.values();

        /*
        {
  "status": 0,
  "msg": "",
  "data": {
    "categories": [
      "苹果", //"+stringUtils.join(tmNames,",")+",其中左右的"要用转义字符
      "三星",
      "华为",
      "oppo",
      "vivo",
      "小米14"
    ],
    "series": [
      {
        "name": "手机品牌",
        "data": [
          8033,
          6758,
          5280,
          6659,
          8113,
          7911
        ]
      }
    ]
  }
}
         */
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                " \"" +//转义字符"
                StringUtils.join(tmNames,"\",\"")//品牌名间用","隔开
                +" \""+//转义字符"
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"手机品牌\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(tmGmvs,",")+//gmv之间用,分隔
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    //获取当天日期
    private int getToday() {
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(yyyyMMdd.format(System.currentTimeMillis()));

    }



//    //http://localhost:8070/sugar/test
//    @RequestMapping("/test")
////    @ResponseBody可以不返回页面
//    public String test1(){
//        System.out.println("刚才有人访问了test");
//        return "success";
//    }
//
//    //http://localhost:8070/sugar/test2?name=zhangsan ==> zhangsan:18
//    //http://localhost:8070/sugar/test2?name=zhangsan&age=20 ==> zhangsan:20
//    @RequestMapping("/test2")
//    public String test2(@RequestParam("name") String nn,//网址输入要输入括号里的,name和nn名字可以不一样,网址要输入name
//                        @RequestParam(value = "age",defaultValue = "18") int age){//不输入默认值是18
//        System.out.println(nn+":"+age);
//        //zhangsan:18
//        //zhangsan:20
//        return "success";
//    }




}
